import gensim
from gensim.models import word2vec, KeyedVectors
import csv
import platform
import sys, os
sys.path.append(os.pardir)
from Utils import load_passages

sentences = []
labels = []
def getAll(csv_name,project_path):
    print("start load passages for w2v")
    if (platform.system() == 'Windows'):
        csv_name = "D:\\DataSet\\"+csv_name+".csv"
    else:
        csv_name = project_path+"/DataSet/"+csv_name+".csv"
    f = open(csv_name, 'r', encoding='utf-8')
    csv_reader = csv.reader(f)

    for row in csv_reader:
        sentences.append(row[1:])
        labels.append(row[0])
    f.close()
    print("load passages for w2v")

def train(name,project_path):
    if (platform.system() == 'Windows'):
        dir_path = "D:\\embedding\\"
        embedding_path = "D:\\embedding\\"+name+".bin"
    else:
        dir_path = project_path+"/embedding/"
        embedding_path = project_path+"/embedding/"+name+".bin"
    if os.path.exists(dir_path):
        print(dir_path + " is existed")
        pass
    else:
        os.makedirs(dir_path)
        print("mkdir "+ dir_path)
    w2v = word2vec.Word2Vec(sentences,window=5,min_count=1,workers=6,size=50)
    print("w2v calculated")
    w2v.wv.save_word2vec_format(embedding_path,binary=True)
    print("w2v saved")

